<?php


namespace app\admin\controller;


use app\service\logService;
use app\service\orderService;
use think\Db;
use think\db\Where;
use think\Exception;

class CxyunPklot extends Common
{
    public function index(){
        $data = request()->get();
        //根据当前账号查询区域
        $com = (new Common())->pidTokenId();
        $whe=new Where;
        $where=array();
        $whe['community_id'] = array('in',$com['community_id']);
        //根据账号查询停车场
        $parkinglist = (new orderService())->parkingList($whe);
        $this->assign('parkinglist',$parkinglist);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $parkingNo[] = $park['parkingNo'];
            }
            //将数组转换成字符串
            $parkingNo = implode(',',$parkingNo);
        }else{
            $parkingNo = null;
        }
        $where=array();
        if (!empty($data)){
            if(!empty($data['parkingNo'])){
                $where[]=['parkingNo','=',$data['parkingNo']];
            }
            if(!empty($data['pklot'])){
                $where[]=['pklot','=',$data['pklot']];

            }
            if(!empty($data['plateNumber'])){
                $where[]=['plateNumber','like','%'.$data['plateNumber'].'%'];

            }
            if(!empty($data['empName'])){
                $where[]=['empName','like','%'.$data['empName'].'%'];
                //$where['empName'] = $data['empName'];//车主电话
            }
            if(!empty($data['empMoblie'])){
                $where[]=['empMoblie','like','%'.$data['empMoblie'].'%'];
                //$where['owner_name'] = $data['owner_name'];//车主电话
            }

            $this->assign('where',$where);
            $where = array_filter($where);
        }else{
            $where=array();
        }

        $page = request()->has('page') ? request()->get('page') : 1;

        $datalist =Db::name('cxyun_pklot')
            ->where('parkingNo','in',$parkingNo)
            ->where($where)
            ->order("id","desc")
            ->paginate(100, false, [
                'page'  =>  $page,
                'query' =>  $where
            ]);

        $number = Db::name('cxyun_pklot')
            ->where('parkingNo','in',$parkingNo)
            ->where($where)
            ->count();
        $this->assign('number',$number);
        $this->assign('list',$datalist);
        $this->assign('datalist',$datalist);
        foreach ($data as $k => $v) {
            $this->assign($k, $v);
        }


        return $this->fetch('index');

    }


    public function add(){
        if(request()->isPOST()){
            $data=request()->post();

            $info=Db::table("cxyun_pklot")
                ->where("parkingNo",$data["parkingNo"])
                ->where("pklot",$data["pklot"])
                ->find();
            if(!empty($info)){
                return json(["code"=>500,"msg"=>"该车位号已经存在"]);
            }
            // if(!$this->isValidChineseLicensePlate($data["plateNumber"])){
            //     return json(["code"=>500,"msg"=>"该车牌号无效"]);
            // }
            $parkingInfo=Db::table("parkinglotinfo")
                ->where("parkingNo",$data["parkingNo"])->find();
            if(empty($parkingInfo)){
                return json(["code"=>500,"msg"=>"车场信息错误"]);
            }
            if(empty($parkingInfo["cxyun_parking_id"])){
                return json(["code"=>500,"msg"=>"cxyun车场信息错误"]);
            }
            $data["startTime"]="2024-01-01 00:00:00";
            $oneYearLater = strtotime("+3 year");
            $data["endTime"]="2027-01-01 00:00:00";
            $data["parkingId"]=$parkingInfo["cxyun_parking_id"];
            $data["create_time"]=date("Y-m-d H:i:s");
            $res=Db::table("cxyun_pklot")->insert($data);
            if($res){
                return json(["code"=>0,"msg"=>"添加成功"]);
            }else{
                return json(["code"=>500,"msg"=>"添加失败，请重试"]);
            }
        }else{
            //根据当前账号查询区域
            $com = $this->pidTokenId();
            $whe=new Where();
            $whe['community_id'] = array('in',implode(',',$com['community_id']));
            $parkinglist = (new orderService())->parkingList($whe);
            $this->assign('parkinglist',$parkinglist);
            return $this->fetch("add");
        }
    }
    
    function isValidChineseLicensePlate($licensePlate) {
        $pattern = '/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽赣贵粤青藏川宁琼使领][A-Z][A-Z0-9]{4}[A-Z0-9挂学警港澳]$/';
        return preg_match($pattern, $licensePlate);
    }

    public function isCarLicense($license)
    {
        //参数判断
        if (empty($license))
        {
            return false;
        }
        //匹配民用车牌和使馆车牌
        //判断标准
        //1.第一位为汉子省份缩写
        //2.第二位为大写字母城市编码
        //3.后面是5位仅含字母和数字的组合
        $regular = "/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新使]{1}[A-Z]{1}[0-9a-zA-Z]{5}$/u";
        preg_match($regular, $license, $match);
        if (isset($match[0]))
        {
            return true;
        }
        //匹配特种车牌(挂,警,学,领,港,澳)
        $regular = '/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]{1}[A-Z]{1}[0-9a-zA-Z]{4}[挂警学领港澳]{1}$/u';
        preg_match($regular, $license, $match);
        if (isset($match[0]))
        {
            return true;
        }
        //匹配武警车牌
        $regular = '/^WJ[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]?[0-9a-zA-Z]{5}$/ui';
        preg_match($regular, $license, $match);
        if (isset($match[0]))
        {
            return true;
        }
        //匹配军牌
        $regular = "/[A-Z]{2}[0-9]{5}$/";
        preg_match($regular, $license, $match);
        if (isset($match[0]))
        {
            return true;
        }
        //匹配新能源车辆6位车牌
        //小型新能源车
        $regular = "/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]{1}[A-Z]{1}[DF]{1}[0-9a-zA-Z]{5}$/u";
        preg_match($regular, $license, $match);
        if (isset($match[0]))
        {
            return true;
        }
        //大型新能源车
        $regular = "/[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新]{1}[A-Z]{1}[0-9a-zA-Z]{5}[DF]{1}$/u";
        preg_match($regular, $license, $match);
        if (isset($match[0]))
        {
            return true;
        }
        return false;
    }


    public function del(){
        $id=request()->get("id");
        $pklot=Db::table("cxyun_pklot")->where("id",$id)->find();

        Db::startTrans();
        try {
            Db::table("cxyun_pklot")->where("id",$id)->delete();
            (new logService())->deletePklot($pklot,"1",session("id"),session("name"));
            //
            Db::commit();
            $this->success('删除成功',url('index'));
            return ;
            return json(["code"=>0,"msg"=>"删除成功"]);
        }catch (Exception $e){
            Db::rollback();
            $this->success($e->getMessage(),url('index'));
            return ;
            return json(["code"=>500,"msg"=>$e->getMessage()]);
        }

    }
}